﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Reflection;

namespace UserLimitedGroupSelection.Code
{
    public class UserLimitedGroupSelectionDialog : PickerDialog
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="UserLimitedGroupSelectionDialog" /> class.
        /// </summary>
        public UserLimitedGroupSelectionDialog()
            : base(new UserLimitedGroupSelectionQuery(), new HierarchyResultControl(), new UserLimitedGroupSelectionEditor(), true)
        {
            //Init results window
            ((HierarchyResultControl)base.ResultControl).PickerInitialData = GetInitialData();
        }

        /// <summary>
        /// Gets the initial data.
        /// </summary>
        /// <returns></returns>
        private SPProviderInitialData GetInitialData()
        {
            //Init initial data class
            //Logic copied from PeoplePickerDialog
            SPProviderInitialData sPProviderInitialData = new SPProviderInitialData();

            sPProviderInitialData.Description = SPResource.GetString("PikcerEmptyResultText", new object[0]);
            sPProviderInitialData.DefaultImageUrl = "Images/PICKERDEFAULT.GIF";
            sPProviderInitialData.DefaultSipUrl = "images/SHAREPOINTFOUNDATION16.PNG";
            sPProviderInitialData.PickerEnabled = true;
            sPProviderInitialData.ResultTypeIsJson = true;

            typeof(PickerDialog).GetProperty("ResultTypeIsJson", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, true, null);

            //Init table columns
            sPProviderInitialData.TableIndexer = new List<string>
	        {
		        UserLimitedGroupSelectionEditor.PeopleInfo.JobTitle,
		        UserLimitedGroupSelectionEditor.PeopleInfo.Department,
		        UserLimitedGroupSelectionEditor.PeopleInfo.Email,
		        UserLimitedGroupSelectionEditor.PeopleInfo.MobilePhone,
		        UserLimitedGroupSelectionEditor.PeopleInfo.AccountName
	        }.ToArray();

            sPProviderInitialData.TableViewColumns = new Dictionary<string, string>
	        {
		        {
			        UserLimitedGroupSelectionEditor.PeopleInfo.JobTitle,
			        SPResource.GetString("PeoplePickerColumnTitle", new object[0])
		        },
		
		        {
			        UserLimitedGroupSelectionEditor.PeopleInfo.Department,
			        SPResource.GetString("PeoplePickerColumnDepartment", new object[0])
		        },
		
		        {
			        UserLimitedGroupSelectionEditor.PeopleInfo.Email,
			        SPResource.GetString("PeoplePickerColumnEmail", new object[0])
		        },
		
		        {
			        UserLimitedGroupSelectionEditor.PeopleInfo.MobilePhone,
			        SPResource.GetString("PeoplePickerColumnMobilePhone", new object[0])
		        },
		
		        {
			        UserLimitedGroupSelectionEditor.PeopleInfo.AccountName,
			        SPResource.GetString("PeoplePickerColumnAccountName", new object[0])
		        }
	        };

            sPProviderInitialData.ResultTypes = new string[]
	        {
		         SPResource.GetString("AllSearch", new object[0])
	        };

            sPProviderInitialData.ResultDisplay = SPDefaultResultDisplayType.TableView;
            sPProviderInitialData.DetailedViewEnabled = false;
            sPProviderInitialData.HierarchialDisplayEnabled = false;

            typeof(PickerDialog).GetProperty("DetailedViewEnabled", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, false, null);
            typeof(PickerDialog).GetProperty("HierarchyDisplayEnabled", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, false, null);

            return sPProviderInitialData;
        }
    }
}